package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.ExtCproduct;
import com.zhanglifan.domain.Factory;
import com.zhanglifan.service.ExtCproductService;
import com.zhanglifan.service.FactoryService;
import com.zhanglifan.util.Page;

import java.util.List;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/10/7 9:05
 * Description:
 */
public class ExtCproductAction extends BaseAction implements ModelDriven<ExtCproduct>{

    private ExtCproduct extCproduct = new ExtCproduct();
    private ExtCproductService extCproductService;
    private FactoryService factoryService;
    private Page<ExtCproduct> page = new Page<>();


    public String tocreate(){
        List<Factory> factoryList = factoryService.find("from Factory where ctype='附件'", Factory.class, null);
        super.put("factoryList",factoryList);
        extCproductService.findPage("from ExtCproduct where contractProduct.id=?",page,ExtCproduct.class,new String[]{extCproduct.getContractProduct().getId()});
        page.setUrl("extCproductAction_tocreate");
        super.push(page);
        return "toCreateSuccess";
    }

    public String insert(){
        extCproductService.saveOrUpdate(extCproduct);
        return tocreate();
    }

    public String toupdate(){
        List<Factory> factoryList = factoryService.find("from Factory where ctype='附件'", Factory.class, null);
        super.put("factoryList",factoryList);
        ExtCproduct extCproduct = extCproductService.get(ExtCproduct.class, this.extCproduct.getId());
        super.push(extCproduct);
        return "toUpdateSuccess";
    }

    public String update(){
        ExtCproduct e = extCproductService.get(ExtCproduct.class, this.extCproduct.getId());
        e.setFactory(factoryService.get(Factory.class,extCproduct.getFactory().getId()));
        e.setFactoryName(extCproduct.getFactoryName());
        e.setProductNo(extCproduct.getProductNo());
        e.setProductImage(extCproduct.getProductImage());
        e.setCnumber(extCproduct.getCnumber());
        e.setPackingUnit(extCproduct.getPackingUnit());
        e.setPrice(extCproduct.getPrice());
        e.setOrderNo(extCproduct.getOrderNo());
        e.setProductDesc(extCproduct.getProductDesc());
        e.setProductRequest(extCproduct.getProductRequest());

        extCproductService.saveOrUpdate(e);

        return tocreate();
    }

    public String delete(){
        extCproductService.deleteById(ExtCproduct.class,extCproduct.getId());
        return tocreate();
    }

    @Override
    public ExtCproduct getModel() {
        return extCproduct;
    }

    public void setExtCproductService(ExtCproductService extCproductService) {
        this.extCproductService = extCproductService;
    }

    public void setFactoryService(FactoryService factoryService) {
        this.factoryService = factoryService;
    }

    public void setPage(Page<ExtCproduct> page) {
        this.page = page;
    }

    public Page<ExtCproduct> getPage() {
        return page;
    }
}
